var $,
    form,
    upload,
    loc = location.href,
    n1 = loc.length,//地址的总长度
    n2 = loc.indexOf('='),//取得=号的位置
    id = decodeURI(loc.substr(n2 + 1, n1 - n2));

var user = JSON.parse(localStorage.getItem('user'));
layui.use(['jquery', 'form', 'upload'], function () {
    $ = layui.jquery;
    form = layui.form;
    upload = layui.upload;

    $.ajax({
        url:'/Course/getAllCourseClasses',
        async: false,
        dataType: 'json',
        success: function (item) {
            item.data.forEach(function(ele, index){
                $('#classId').append(new Option(ele.title, ele.categroyId));// 下拉菜单里添加元素
            });
            form.render("select");
        }
    });


    //修改图片
    upload.render({
        elem: '#updateImg'
        ,url: '/Upload/uploadImages'
        ,auto: false
        ,bindAction: '#subUpload'
        ,accept: 'images'
        ,field: 'files'
        ,choose:function (obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $('#url').html('<img src="'+result+'" width="186px" height="186px"/>');
            });
        }
        ,done: function(res){
            //上传完毕
            $('input[name=url]').val(res.data);
        }
    });

    //修改视频
    upload.render({
        elem: '#updateVideo'
        ,url: '/Upload/uploadImages'
        ,auto: false
        ,bindAction: '#Upload'
        ,accept: 'video'
        ,field: 'files'
        ,choose:function (obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                console.log(result);
                $('#courseUrl').html('<video src="'+result+'" width="186px" controls height="186px"/>');
            });
        }
        ,done: function(res){
            //上传完毕
            $('input[name=courseUrl]').val(res.data);
        },before: function (obj) {
            // alert(1)
            // console.log(obj);
        }
    });
});

var coursePrice;

//赋值
$.ajax({
    url:'/Course/getCourseById',
    type: 'post',
    dataType: 'json',
    data: {
        'id' : id,
        'type' : 2
    },
    success: function (item) {
        $('#url').html('<img src="'+item.data.url+'" width="186px" height="186px"/>');
        $('#courseUrl').html('<video src="'+item.data.courseUrl+'" controls width="186" height="186">');
        $('input[name=courseName]').val(item.data.courseName);
        coursePrice = item.data.courseNprice;
        $('input[name=courseNprice]').val(item.data.courseNprice);
        $('input[name=courseIntro]').val(item.data.courseIntro);
        $('select[name=classId]').text(item.data.classId);
    }
});

//修改
function determine () {
    console.log(coursePrice)
    //触发上传
    $('#subUpload').click();
    $('#Upload').click();

    setTimeout(function (){
        //获取时间
        var date = getDate();
        data = form.val('formTest');
        data.updateId = user.userId;
        data.updateDate = date;
        data.coursePrice = coursePrice;
        data.courseId = id;
        data.courseStatus = 2;
        data.courseCstatus = 4;
        data.userId = user.userId;
        data.type = 2;
        $.ajax({
            url:'/Course/updateCourse',
            type: 'post',
            dataType:'json',
            data: data,
            success:function (data) {
                if(data.data){
                    //添加的操作日志
                    $.ajax({
                        url:'/Product/addProductOperationRecord',
                        dataType:'json',
                        data:{
                            "operationType": "2",
                            "executionTime": date,
                            "remarks": "修改课程信息",
                            "updateId": user.userId,
                            "operationList": 'js_course',
                            "type": 2
                        }
                    });
                    layer.msg("修改成功");
                    cancel();
                    parent.getTableData();
                }else{
                    layer.msg("修改失败");
                }
            }
        });
    }, 2000 );
}

//获取时间
function getDate() {
    var date = new Date().toLocaleString('zh', { hour12: false }).split('/').join('-');
    var temp = date.substring(0, 9);
    date = date.substr(date.lastIndexOf('-') + 3);
    date = temp + date;
    return date;
;}

//关闭
function cancel() {
    var index = parent.layer.index;
    parent.layer.close(index);
}